﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using mshtml;
using FastPoster.Helpers;
using System.Windows.Forms;
using FastPoster.MainForms;
using System.Web;

namespace FastPoster.MainClasses
{
    public class HTMLForm
    {
        public string           Action { get; set; }
        public string           Method { get; set; }
        public Hashtable        Inputs = new Hashtable();
        public static FrmKetQuaDangTin OwnerFrm { get; set; }

        public HTMLForm()
        {
            Action = "";
            Method = "";
        }

        public HTMLForm(string boxlink, IHTMLFormElement element)
        {
            this.Action = element.method;
            this.Method = element.action;
            IHTMLElementCollection elements = (IHTMLElementCollection)element.tags("input");
            GlobalVariables.Logger.Info("Init HTMLForm for box " + boxlink + ": started");

            foreach (IHTMLInputElement element2 in elements)
            {
                try
                {
                    string name = element2.name;
                    string str2 = element2.value;
                    if (name == null)
                    {
                        name = element2.type;
                    }
                    this.Inputs.Add(name, str2);
                    GlobalVariables.Logger.Info("Init IHTMLInputElement for box " + boxlink + "- name: " + element2.name + " - value: " + element2.value);
                    continue;
                }
                catch
                {
                    GlobalVariables.Logger.Info("Init IHTMLInputElement for box " + boxlink + ": failed ");
                    continue;
                }
            }
        }

        public delegate HTMLForm[] GetAllFormsDel(string boxlink, string html);

        public static HTMLForm[] GetAllForms(string boxlink, string html)
        {
            GlobalVariables.Logger.Info("GetAllForms for box " + boxlink + ": started");
            List<HTMLForm> list = new List<HTMLForm>();
            try
            {
                if (html.Contains("</form>"))
                {
                    GlobalVariables.Logger.Info("GetAllForms for box " + boxlink + "- html.Contains(\"</form>\"): true");

                    IHTMLDocument2 htmlDoc = new HTMLDocumentClass();
                    try
                    {
                        htmlDoc.clear();
                        htmlDoc.write(new object[] { HttpUtility.HtmlDecode(html) });
                        htmlDoc.close();
                        GlobalVariables.Logger.Info("GetAllForms for box " + boxlink + "- document.forms.length: " + htmlDoc.forms.length);
                    }
                    catch(Exception ex)
                    {
                        if (ex.InnerException != null)
                        {
                            GlobalVariables.Logger.Error("GetAllForms for box " + boxlink + ": failed - error: " + ex.InnerException.Message);
                        }
                        else
                        {
                            GlobalVariables.Logger.Error("GetAllForms for box " + boxlink + ": failed - error: " + ex.Message);
                        }
                        return list.ToArray();
                    }

                    foreach (IHTMLFormElement element in htmlDoc.forms)
                    {
                        GlobalVariables.Logger.Info("Add IHTMLFormElement for box " + boxlink + ": started");
                        HTMLForm frm = new HTMLForm(boxlink, element);
                        frm.Action = element.action;
                        frm.Method = element.method;
                        list.Add(frm);
                        GlobalVariables.Logger.Info("Add IHTMLFormElement for box " + boxlink + ": done");
                    }
                }
                else
                {
                    GlobalVariables.Logger.Info("GetAllForms for box " + boxlink + " - html.Contains(\"</form>\"): false");
                }
                GlobalVariables.Logger.Info("GetAllForms for box " + boxlink + ": done");
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error("GetAllForms for box " + boxlink + ": failed - error: " + ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error("GetAllForms for box " + boxlink + ": failed - error: " + ex.Message);
                }
            }
            return list.ToArray();
        }

        public static HTMLForm GetFormByAction(string boxlink, string html, string action)
        {
            try
            {
                GlobalVariables.Logger.Info("GetFormByAction for box " + boxlink + ": started");
                HTMLForm[] htmlforms = GetAllForms(boxlink, html);
                foreach (HTMLForm form in htmlforms)
                {
                    if (form.Action != null)
                    {
                        if (form.Action.ToLower().Contains(action.ToLower()))
                        {
                            GlobalVariables.Logger.Error("GetFormByAction for box " + boxlink + ": done");
                            return form;
                        }
                    }
                }
                GlobalVariables.Logger.Error("GetFormByAction for box " + boxlink + ": failed");
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error("GetFormByAction for box " + boxlink + ": failed - error: " + ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error("GetFormByAction for box " + boxlink + ": failed - error: " + ex.Message);
                }      
            }
            return null;
        }

        public string ToPostData()
        {
            try
            {
                StringBuilder builder = new StringBuilder();
                foreach (string str in this.Inputs.Keys)
                {
                    object obj2 = this.Inputs[str];
                    string text = (obj2 == null) ? "" : obj2.ToString();
                    builder.AppendFormat("{0}={1}&", VBB.UrlEncode(str), VBB.UrlEncode(text));
                }
                if (builder.Length > 1)
                {
                    return builder.ToString().Substring(0, builder.Length - 1);
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error("Error on posting: " + ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error("Error on posting: " + ex.Message);
                }
            }
            return string.Empty;
        }
    }
}
